package com.alibaba.citrus.cr.platform.enhance.facade.service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.bcorder.model.dto.BaselineQueryChannelByPageRequest;
import com.epoch.app.bcorder.service.BaseDataService;
import com.epoch.app.crplatformenhance.channel.service.ChannelService;
import com.epoch.app.crplatformenhance.model.dto.ChannelResponse;
import com.epoch.app.crplatformenhance.model.dto.QueryChannelRequest;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: qianlei
 * @create: 2021/11/06 16:16
 */
@Primary
@Component
public class ChannelServiceImpl implements ChannelService {
    @Autowired
    BaseDataService baseDataService;

    @Override
    @FacadeInvoker
    public Result<ChannelResponse> queryChannelDetail(QueryChannelRequest queryChannelRequest){
        BaselineQueryChannelByPageRequest baselineBatchQueryChannelsRequest = new BaselineQueryChannelByPageRequest();
        baselineBatchQueryChannelsRequest.setChannelCode(queryChannelRequest.getChannelCode());
        baselineBatchQueryChannelsRequest.setChannelName(queryChannelRequest.getChannelName());
        baselineBatchQueryChannelsRequest.setChannelType(queryChannelRequest.getChannelType());
        baselineBatchQueryChannelsRequest.setOutCode(queryChannelRequest.getOutCode());
        baselineBatchQueryChannelsRequest.setStatus(queryChannelRequest.getStatus());

        //调用基线API获取sap渠道编码
        Map map = (Map) baseDataService.baselineQueryChannelByPage(baselineBatchQueryChannelsRequest);
        ChannelResponse channelResponse = new ChannelResponse();


        if(Objects.nonNull(map)){
            Map<String,Object> resultMap = (Map<String, Object>) map.get("result");
            if(Objects.nonNull(resultMap)){
                List<Map<String,Object>> resultList = (List<Map<String, Object>>) resultMap.get("content");
                if(CollectionUtils.isNotEmpty(resultList)){
                    Map<String, Object> resultSubMap = resultList.get(0);
                    channelResponse.setOutCode((String) resultSubMap.get("outCode"));
                    channelResponse.setChannelName((String) resultSubMap.get("channelName"));
                    channelResponse.setChannelCode((String) resultSubMap.get("channelCode"));
//                    channelResponse.setChannelType((Integer) resultSubMap.get("channelType"));
                    channelResponse.setChannelId((String) resultSubMap.get("channelId"));
                }

            }
        }
        return Result.success(channelResponse);
    }

}
